package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.commons.utils.UserHolder;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.model.domain.mysql.Question;
import com.tanhua.model.domain.mysql.Settings;
import com.tanhua.model.domain.mysql.User;
import com.tanhua.model.domain.mysql.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @Autowired
    private LoginService loginService;

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private BlackListApi blackListApi;

    public SettingsVo settings() {
        SettingsVo settingsVo = new SettingsVo();
        Long userId = UserHolder.getUserId();
        User user = userApi.findById(userId);
        settingsVo.setPhone(user.getMobile());
        //
        Question question = questionApi.findQuestion(userId);
        String strangerQuestion = question == null ? "看nm,滚" : question.getTxt();
        settingsVo.setStrangerQuestion(strangerQuestion);
        //
        Settings settings = settingsApi.findSettings(userId);
        if (settings != null) {
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }
        return settingsVo;
    }

    public void saveQuestions(String content) {
        Long userId = UserHolder.getUserId();
        Question question = questionApi.findQuestion(userId);
        if (question == null) {
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.saveQuestions(question);
            return;
        }
        question.setTxt(content);
        questionApi.updateQuestions(question);
    }

    public void saveNotificationsSettings(Settings settings) {
        Long userId = UserHolder.getUserId();
        Settings findSettings = settingsApi.findSettings(userId);
        if (findSettings == null) {
            findSettings = new Settings();
            findSettings.setUserId(userId);
            findSettings.setGonggaoNotification(settings.getGonggaoNotification());
            findSettings.setLikeNotification(settings.getLikeNotification());
            findSettings.setPinglunNotification(settings.getPinglunNotification());
            settingsApi.saveSettings(findSettings);
            return;
        }
        findSettings.setGonggaoNotification(settings.getGonggaoNotification());
        findSettings.setLikeNotification(settings.getLikeNotification());
        findSettings.setPinglunNotification(settings.getPinglunNotification());
        settingsApi.updateSettings(findSettings);
    }

    public PageResult findBlacklist(int page, int pageSize) {
        //1、获取当前用户的id
        Long userId = UserHolder.getUserId();
        //2、调用API查询用户的黑名单分页列表  Ipage对象
        IPage<UserInfo> iPage = blackListApi.findBlacklist(userId, page, pageSize);
        //3、对象转化，将查询的Ipage对象的内容封装到PageResult中
        PageResult pr = new PageResult(page, pageSize, (int) iPage.getTotal(), iPage.getRecords());
        //4、返回
        return pr;
    }

    public void deleteFromBlackList(Long blackUserId) {
        //1、获取当前用户id
        Long userId = UserHolder.getUserId();
        //2、调用api删除
        blackListApi.delete(userId, blackUserId);
    }

    public Boolean updatePhoneOfcheckCode(String verificationCode) {
        String mobile = UserHolder.getMobile();
        loginService.checkVerificationCode(mobile, verificationCode);
        return true;
    }

    public void updatePhone(String phone) {
        Long userId = UserHolder.getUserId();
        User user = new User();
        user.setId(userId);
        user.setMobile(phone);
        userApi.update(user);
    }
}
